/* TRN
Copyright (c) 2010, jonoerik
All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
 conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
	disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
	disclaimer in the documentation and/or other materials provided with the distribution.
    * The names of contributors may not be used to endorse or promote products derived from this software without specific
	prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE. */

#include "includes.hpp"

actor::actor(int nindex, int nsx, int nsy, dir nsfacing, TCODColor ncol, char nkup, char nkleft, char nkdown, char nkright)
{
    index = nindex;
    startx = nsx;
    starty = nsy;
    startfacing = nsfacing;
    col = ncol;
    kup = nkup;
    kdown = nkdown;
    kleft = nkleft;
    kright = nkright;
    useai = false;
}

actor::actor(int nindex, int nsx, int nsy, dir nsfacing, TCODColor ncol)
{
    index = nindex;
    startx = nsx;
    starty = nsy;
    startfacing = nsfacing;
    col = ncol;
    useai = true;
}


void actor::init()
{
    alive = true;
    x = startx;
    y = starty;
    facing = startfacing;
    
    gamegrid->set(x, y, index); //required for first step, as actor is moved before first real gamegrid->set
}

void actor::dostep(TCOD_key_t key)
{
    if (alive) {
        if (useai) {
            moveai();
        } else {
            if (key.c == kup && facing != down) {facing = up;}
            if (key.c == kdown && facing != up) {facing = down;}
            if (key.c == kleft && facing != right) {facing = left;}
            if (key.c == kright && facing != left) {facing = right;}
        }

        switch (facing) {
            case up:
                move(0, -1);
                break;
            case down:
                move(0, 1);
                break;
            case left:
                move(-1, 0);
                break;
            case right:
                move(1, 0);
                break;
        }
    }
}

void actor::draw(TCODConsole *con)
{
    switch (facing) {
        case up:
            con->setChar(x, y, TCOD_CHAR_ARROW2_N);
            break;
        case down:
            con->setChar(x, y, TCOD_CHAR_ARROW2_S);
            break;
        case left:
            con->setChar(x, y, TCOD_CHAR_ARROW2_W);
            break;
        case right:
            con->setChar(x, y, TCOD_CHAR_ARROW2_E);
            break;
    }
    con->setFore(x, y, TCODColor::black);
}

int actor::drawinfo(TCODConsole *con, int cx, int cy)
{
    char cstr[32];
    TCODConsole::setColorControl(TCOD_COLCTRL_1, col, TCODColor::black);

    sprintf(cstr, "%%cPlayer %d%%c", index + 1);
    con->printLeft(cx, cy, TCOD_BKGND_NONE, cstr, TCOD_COLCTRL_1, TCOD_COLCTRL_STOP);

    if (useai) {
        sprintf(cstr, "%%cComputer Controlled%%c");
        con->printLeft(cx, cy+2, TCOD_BKGND_NONE, cstr, TCOD_COLCTRL_1, TCOD_COLCTRL_STOP);
        return 3;
    } else {
        sprintf(cstr, "%%c  %c%%c", kup);
        con->printLeft(cx, cy+2, TCOD_BKGND_NONE, cstr, TCOD_COLCTRL_1, TCOD_COLCTRL_STOP);

        sprintf(cstr, "%%c%c %c %c%%c", kleft, kdown, kright);
        con->printLeft(cx, cy+3, TCOD_BKGND_NONE, cstr, TCOD_COLCTRL_1, TCOD_COLCTRL_STOP);
        return 4;
    }
}

void actor::move(int dx, int dy)
{
    if (alive) {
        x+=dx;
        y+=dy;
        if (gamegrid->get(x, y) != -2) {
            alive = false;
        } else {
            gamegrid->set(x, y, index);
        }
    }
}

void actor::moveai()
{
    if (!isclearfront()) { //forced to turn
        if (isclearleft() && !isclearright()) {
            turnleft();
        } else if (isclearright() && !isclearleft()) {
            turnright();
        } else if (isclearleft() && isclearright()) {
            //choose the direction that leaves the most space avaliable
            int arealeft = getarealeft();
            int arearight = getarearight();
            if (arealeft == arearight) {
                //turns are equare, so take a random one
                if (TCODRandom::getInstance()->getInt(0,1) == 0) {
                    turnleft();
                } else {
                    turnright();
                }
            } else if (arealeft > arearight) {
                turnleft();
            } else if (arearight > arealeft) {
                turnright();
            }
        }
        //if can't turn in either direction, just crash straight ahead
    } else { //random turn
        if (TCODRandom::getInstance()->getInt(0,70) == 0) {
            if (isclearleft() && isclearright()) {
                if (TCODRandom::getInstance()->getInt(0,1) == 0) {
                    turnleft();
                } else {
                    turnright();
                }
            } else if (isclearleft()) {
                turnleft();
            } else if (isclearright()) {
                turnright();
            }
        }
    }
}

bool actor::isclearfront()
{
    switch (facing) {
        case up:
            return gamegrid->get(x, y - 1) == -2;
            break;
        case down:
            return gamegrid->get(x, y + 1) == -2;
            break;
        case left:
            return gamegrid->get(x - 1, y) == -2;
            break;
        case right:
            return gamegrid->get(x + 1, y) == -2;
            break;
    }
}

bool actor::isclearleft()
{
    switch (facing) {
        case up:
            return gamegrid->get(x - 1, y) == -2;
            break;
        case down:
            return gamegrid->get(x + 1, y) == -2;
            break;
        case left:
            return gamegrid->get(x, y + 1) == -2;
            break;
        case right:
            return gamegrid->get(x, y - 1) == -2;
            break;
    }
}

bool actor::isclearright()
{
    switch (facing) {
        case up:
            return gamegrid->get(x + 1, y) == -2;
            break;
        case down:
            return gamegrid->get(x - 1, y) == -2;
            break;
        case left:
            return gamegrid->get(x, y - 1) == -2;
            break;
        case right:
            return gamegrid->get(x, y + 1) == -2;
            break;
    }
}

void actor::turnleft()
{
    switch (facing) {
        case up:
            facing = left;
            break;
        case down:
            facing = right;
            break;
        case left:
            facing = down;
            break;
        case right:
            facing = up;
            break;
    }
}

void actor::turnright()
{
    switch (facing) {
        case up:
            facing = right;
            break;
        case down:
            facing = left;
            break;
        case left:
            facing = up;
            break;
        case right:
            facing = down;
            break;
    }
}

int actor::getarea(int cx, int cy)
{
    grid *tmpgrid = gamegrid->copysimple();
    tmpgrid->set(cx, cy, 2);
    bool changemade; //was a change made last flood fill iteration
    do { //floodfill
        changemade = false;
        for (int iy = 0; iy < tmpgrid->height; iy++) {
            for (int ix = 0; ix < tmpgrid->width; ix++) {
                if (tmpgrid->get(ix, iy) == 0) {
                    //check if an adjacent cell is 2
                    if (tmpgrid->get(ix - 1, iy) == 2 || tmpgrid->get(ix + 1, iy) == 2 || tmpgrid->get(ix, iy - 1) == 2 || tmpgrid->get(ix, iy + 1) == 2) {
                        tmpgrid->set(ix, iy, 2);
                        changemade = true;
                    }
                }
            }
        }
    } while (changemade);
    
    //count cells set to 2
    int total = 0;
    for (int iy = 0; iy < tmpgrid->height; iy++) {
        for (int ix = 0; ix < tmpgrid->width; ix++) {
            if (tmpgrid->get(ix, iy) == 2) {
                total++;
            }
        }
    }

    delete tmpgrid;
    return total;
}

int actor::getarealeft()
{
    switch (facing) {
        case up:
            return getarea(x - 1, y);
            break;
        case down:
            return getarea(x + 1, y);
            break;
        case left:
            return getarea(x, y + 1);
            break;
        case right:
            return getarea(x, y - 1);
            break;
    }
}

int actor::getarearight()
{
    switch (facing) {
        case up:
            return getarea(x + 1, y);
            break;
        case down:
            return getarea(x - 1, y);
            break;
        case left:
            return getarea(x, y - 1);
            break;
        case right:
            return getarea(x, y + 1);
            break;
    }
}
